//#########################################################################
//                              WackiEngie
//           Copyright (c) 2014 Marc Wacker. All rights reserved.
//#########################################################################

#include "WeTimer.h"
#include "windows.h"


using namespace WackiEngine;

Timer::Timer()
: _ticks(0), _pticks(0), _totalTicks(0), _seconds(0), _totalSeconds(0)
{
	//	get frequency/initialize current ticks:
#ifdef _WIN32
	LARGE_INTEGER tmp;
	QueryPerformanceFrequency(&tmp);
	_freq = (int64)tmp.QuadPart;
	QueryPerformanceCounter(&tmp);
	_cticks = (int64)tmp.QuadPart;
#else
	_freq = (int64)CLOCKS_PER_SEC;
	_cticks = (int64)clock();
#endif

	//	get 1/frequency:
	_rfreq = 1.0 / (double)_freq;
}

void Timer::update() {
	_pticks = _cticks;

	//	get new value for current ticks:
#ifdef _WIN32
	LARGE_INTEGER tmp;
	QueryPerformanceCounter(&tmp);
	_cticks = (int64)tmp.QuadPart;
#else
	_cticks = (BigIntT)clock();
#endif

	//	calculate deltas:
	_ticks = _cticks - _pticks;
	_totalTicks += _ticks;
	_seconds = (double)_ticks * _rfreq;
	_totalSeconds += _seconds;
}

void Timer::reset() {
	_ticks = 0;
	_totalTicks = 0;
	_seconds = 0;
	_totalSeconds = 0;

	//	get new value for current ticks:
#ifdef _WIN32
	LARGE_INTEGER tmp;
	QueryPerformanceCounter(&tmp);
	_cticks = (int64)tmp.QuadPart;
#else
	_cticks = (BigIntT)clock();
#endif
}
